import 'package:courseapp/entity/coursedto.dart';
import 'package:courseapp/entity/oncecoursedto.dart';
import 'package:courseapp/http/HttpUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TodayCourseWidget extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TodayCourseState();
  }

}

class TodayCourseState extends State<StatefulWidget> {

 List<String> week = ["周一","周二","周三","周四","周五"];
 Coursedto coursedto = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    HttpUtil.getInstance().postTodayStuCourse().asStream().listen((course){
      setState(() {
        this.coursedto = course;
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    if(coursedto == null) {
      // TODO: implement build
      return Scaffold(
        body: Container(alignment:Alignment.center,child:Text("今日课程")),
      );
    } else {
      return Scaffold(
        backgroundColor: Colors.white,
        body: ListView.builder(itemCount: coursedto.data.length,
        itemBuilder: (context,index){
          String courseName = coursedto.data[index].courseName;
          String weekDay =  week[coursedto.data[index].whatDay - 2];

          return
            FlatButton(
            color: Colors.white,
            padding: EdgeInsets.all(0),
            onPressed: (){
              showSignDialog(coursedto.data[index].id);
            },
            child: DecoratedBox(
                decoration: BoxDecoration( color: Colors.white, border: Border(bottom: Divider.createBorderSide(context, color: Colors.grey),)),
                child:Container(padding: EdgeInsets.all(5) ,child: Column(children: <Widget>[
                     Container(alignment: Alignment.centerLeft, child:Text("课程:$courseName")),
                     Container(alignment: Alignment.centerLeft, child:Text("时间:$weekDay")),],)))
            );

        },
        itemExtent: 50,
        )
      );
    }

  }

  Future<void> showSignDialog(int courseId) async {
    Oncecoursedto oncecoursedto = await HttpUtil.getInstance().queryCourse(courseId);
    if(!oncecoursedto.data.canSign) {
      showDialog(context: context,child: Container(alignment: Alignment.center,child:
      SizedBox(
        width: 100,
        height:100,
        child: FlatButton(
          child: Text("尚未开始",style: TextStyle(color: Colors.white70),),
          color: Colors.grey,
          shape: CircleBorder(
            side: BorderSide(color: Colors.white70),
          ),
          onPressed: (){
            Navigator.of(context).pop();},
        ),
      )
      ));
    } else {
      showDialog(context: context,child: Container(alignment: Alignment.center,child:
      SizedBox(
      width: 100,
      height:100,
      child: FlatButton(
        child: Text("开始签到",style: TextStyle(color: Colors.white),),
        color: Colors.pink,
        shape: CircleBorder(
          side: BorderSide(color: Colors.pink),
        ),
        onPressed: (){

          Navigator.of(context).pop();},
      ),
      )
      ));
    }

  }


}